<?php
//!defined('PH')&&exit('Forbidden.');
class File_Manage {
	static $_instance;
	var $baseDir="";
	var $activeDir="";
	var $purview=0755;
	var $allowDeleteDir=false;//如果是0则不允许删除目录，如果允许请设置为1
	var $isSafeMode=false;	//是否启用安全模式
	var $ftp_mkdir=false;	//是否启用ftp方式
	var $totalsize;			//存储计算文件大小
	var $g_ftpLink;
	
	function __construct(){
		
	}
	public static function getInstance(){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new File_Manage();
		}else{
			return self::$_instance;
		}
	}
	function RenameFile($oldname,$newname){		//更改文件名
		$oldname = $this->baseDir.$this->activeDir."/".$oldname;
		$newname = $this->baseDir.$this->activeDir."/".$newname;
		if(($newname!=$oldname) && is_writable($oldname)){
			rename($oldname,$newname);
		}
	}
	
	function AddDir($dirname,$mmode){		//创建新目录
		$newdir = $dirname;
		$dirname = $this->baseDir.$this->activeDir."/".$dirname;
		if(is_writable($this->baseDir.$this->activeDir)){
			if($this->isSafeMode || $this->ftp_mkdir){
				return $this->FtpMkdir($truepath,$mmode);
				$this->CloseFtp();
			}else{
				if(!file_exists($diename)){
					mkdir($dirname,$mmode);
					return true;
				}else{
					return true;
				}
			}	
		}else{
			die("创建新目录失败，因为这个位置不允许写入:".$this->baseDir.$this->activeDir);
			return;
		}
	}
	
	function FtpMkdir($truepath,$mmode,$isMkdir=true){
		$this->OpenFtp();
		$mdir = ereg_replace('/^[\\|\/]/','',$truepath);
		if($isMkdir){
			$this->ftp_mkdir($g_ftpLink,$mdir);
		}
		return ftp_site($this->g_ftpLink,"chmod $mmode $mdir");
	}
	
	function FtpChmod($truepath,$mmode){
		return $this->FtpMkdir($truepath,$mmode,false);
	}

	function OpenFtp($ftp_host,$ftp_port,$ftp_user,$ftp_pwd){
		if(!isset($this->g_ftpLink)){
			$this->g_ftpLink = ftp_connect($ftp_host,$ftp_port);
			if(!$this->g_ftpLink){
				echo "连接FTP失败！";
				exit();
			}
			if(!ftp_login($this->g_ftpLink,$ftp_user,$ftp_pwd)){
				echo "登陆FTP失败！";
				exit();
			}
		}
	}

	function CloseFtp(){
		if($this->g_ftpLink){
			@ftp_quit($this->g_ftpLink);
		}
	}

	function MkdirAll($truepath,$mmode){
		if($this->isSafeMode || $this->ftp_mkdir){
			return $this->FtpMkdir($truepath,$mmode);
		}else{
			if(!file_exists($truepath)){
				mkdir($truepath,$dir_purview);
				chmod($truepath,$dir_purview);
				return true;
			}else{
				return true;
			}
		}
	}

	function ChmodAll($truepath,$mmode){
		global $ftp_mkdir,$isSafeMode;
		if($this->isSafeMode || $this->ftp_mkdir){
			return $this->FtpChmod($truepath,$mmode);
		}else{
			return chmod($truepath,'0'.$mmode);
		}
	}
	
	//移动文件
	function MoveFile($mfile,$mpath){
		if($mpath!="" && !ereg("\.\.",$mpath)){
			$oldfile = $this->baseDir.$this->activeDir."/$mfile";
			$mpath = str_replace("\\","/",$mpath);
			$mpath = ereg_replace("/{1,}","/",$mpath);
			if(!ereg("^/",$mpath)){
				$mpath = $this->activeDir."/".$mpath;
			}
			$truepath = $this->baseDir.$mpath;
			if(is_readable($oldfile) && is_readable($truepath) && is_writable($truepath)){
				if(is_dir($truepath)){
					copy($oldfile,$truepath."/$mfile");
				}else{
					$this->AddDir($truepath,$this->purview);
					copy($oldfile,$truepath."/$mfile");
				}
				unlink($oldfile);
				die("成功移动文件:$mpath");
				return true;
			}else{
				die("移动文件 $oldfile -&gt; $truepath/$mfile 失败，可能是某个位置权限不足:$mpath");
				return false;
			}
		}else{
			die("对不起，你移动的路径不合法！");
			return ;
		}
	}

 	//删除目录
	function RmDirFiles($indir){
		if(!is_dir($indir)){
			return ;
		}
		$dh = dir($indir);
		while($filename = $dh->read()){
			if($filename == "." || $filename == ".."){
				continue;
			}else if(is_file("$indir/$filename")){
				@unlink("$indir/$filename");
			}else{
				$this->RmDirFiles("$indir/$filename");
			}
		}
		$dh->close();
		@rmdir($indir);
	}
	//删除文件
	function DeleteFile($filename){
		$filename = $this->baseDir.$this->activeDir."/$filename";
		if(file_exists($filename)){
			if(is_file($filename)){
				@unlink($filename);
			}else{
				if($this->allowDeleteDir){
					$this->RmDirFiles($filename);
				}
			}
			return true;
		}else{
			return false;
		}
	}
	
	//获取目录文件大小
	function checksize($indir){
		if(!is_dir($indir)){
			die("没找到目录，请确定路径是否存在");
			return ;
		}
		$dh=dir($indir);
		while($filename=$dh->read()){
			if(!ereg("^\.",$filename)){
				if(is_dir("$indir/$filename")){
					$this->checksize("$indir/$filename");
				}else{
					$this->totalsize=$this->totalsize + filesize("$indir/$filename");
				}
			}
		}
	}
	
}
?>